package com.bgs.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bgs.listener.StuExcelListener;
import com.bgs.pojo.*;
import com.bgs.pojo.data.stuInfoData;
import com.bgs.pojo.vo.studentInformationVO;
import com.bgs.pojo.vo.uVO;
import com.bgs.service.ERegisterService;
import com.bgs.service.StudenService;
import com.bgs.utils.MyContent;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther: 梁玉杰
 * @Date: 2022/3/21 16:54
 * @Description: TODO
 */
@RestController
@RequestMapping("/studentVue")
@Log4j2
@CrossOrigin
public class StudentVueController {


    @Autowired
    private StudenService studenService;

    //学生注册
    @Autowired
    private ERegisterService eRegisterService;


    //全查+模糊差------新技术（因为需要返回map集合，但是呢，不推荐用map）
    @RequestMapping("/StudentfindAllormodil")
//    public R StudentfindAllormodil(@RequestBody(required = false) studentInformationVO sVO){
    public R StudentfindAllormodil(@RequestBody(required = false) studentInformationVO sVO){

        //模糊查询前端传过来的数据
        log.info("模糊查询前端传过来的数据studentInformationVO"+sVO);
//        Map<String,Object> m = new HashMap<>();
        //设置分页信息(当前页码，每页显示数量)
//        PageHelper.startPage(sVO.getPage(),sVO.getLimit());
        PageHelper.startPage(sVO.getPage(),sVO.getLimit());
        //调用分页查询账单类列表的方法
        studentInformationVO svo = new studentInformationVO();
        List<studentInformation> slist = studenService.findAll(sVO);
        //创建分页对象
        PageInfo<studentInformation> pageInfo = new PageInfo<studentInformation>(slist);
        // JSONObject jsonObj = JSONObject.parseObject(sJson);
        // m = jsonObj; json对象转成map
        log.info("List<studentInformation>:::"+pageInfo);
//        m.put(MyContent.CODE,20000);
//        m.put("success",true);
//        m.put(MyContent.MSG,"查询成功！");
//        Map<String, Object> data = new HashMap<String, Object>();
//        data.put("total",pageInfo.getTotal());
//        data.put("rows",pageInfo.getList());
        return  R.ok().data("total",pageInfo.getTotal()).data("rows",pageInfo.getList());
    }

    @PostMapping("/addStu")
    public R addStu(@RequestBody studentInformation studentInformation){
        studentInformation.setStuId( String.valueOf((int)(((Math.random()*9+1)*100000))));
        log.info("添加的"+studentInformation);
        boolean flag = (studenService.insertSelective(studentInformation) > 0);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }

    }

    //修改
    @PostMapping("/updStu")
    public R updStu(@RequestBody studentInformation stu){
        log.info("修改的VO"+stu);
        boolean flag = (studenService.updateByPrimaryKeySelective(stu) > 0);
            if (flag){
                return R.ok();
            }else {
                return R.error();
            }
    }

    @GetMapping("/getstu/{id}")
    public R getTeacher(@PathVariable String id){
        studentInformation eduTeacher = studenService.selectByPrimaryKey(Integer.valueOf(id));
        return R.ok().data("teacher",eduTeacher);
    }

    /**
     * 删除学生
     */
    @RequestMapping("/deleteById/{stuId}")
    public R deleteById(@PathVariable String stuId){
        System.out.println(stuId);
        boolean flag =(studenService.deleteById(Integer.valueOf(stuId))>0);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 批量删除账单 前端是字符串
     */
    @RequestMapping("/batchDeletestr")
    public R batchDeletestr(@RequestBody String ids){
        int count = 0;
        //将字符串拆分成数组
//        String[] idsStr = ids.split(",");
        JSONObject jsonObject = JSONObject.parseObject(ids);
        JSONArray arrId = jsonObject.getJSONArray("ids");
        for (int i = 0; i < arrId.size(); i++) {
//            System.out.printf("dsdss"+arrId.getInteger(i));
//            log.info("批量删除"+arrId.getInteger(i));
            int a = studenService.deleteById(arrId.getInteger(i));
            if(a>0){
               count++;
            }
        }
        //判断受影响行数是否为0
        if (count>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 批量删除账单 前端是数组
     */
    @DeleteMapping("/batchDeletelist/{ids}")
    public R batchDeletelist(@PathVariable Integer[] ids){
        int count = 0;
//        System.out.println("前端传入的数组：："+ids);
        for (int i = 0; i < ids.length; i++) {
//            System.out.println("前端传入的数组：："+ids[i]);
            int a = studenService.deleteById(ids[i]);
            if(a>0){
                count++;
            }
        }
        //判断受影响行数是否为0
        if (count>0){
            return R.ok();
        }else {
            return R.error();
        }
    }




    //查询添加与修改的省下拉框
    @GetMapping("/provinces")
    public R provinces(){
        List<Provinces> plist = studenService.provinces();
        log.info("pppppppppppppppp"+plist);
        return R.ok().data("items",plist);
    }

    //查询添加与修改的市下拉框
    @GetMapping("/cities")
    public R cities(){
        List<Cities> clist = studenService.cities();

        log.info("ccccccccc"+clist);
        return R.ok().data("items",clist);

    }

    //查询添加与修改的县/区下拉框
    @GetMapping("/areas")
    public R areas(){
        List<Areas> alist = studenService.areas();
        log.info("aaaaaaaaa"+alist);
        return R.ok().data("items",alist);
    }




    //全查+模糊差------新技术（因为需要返回map集合，但是呢，不推荐用map）
    @PostMapping("/ufindAllormodil")
//    public R StudentfindAllormodil(@RequestBody(required = false) studentInformationVO sVO){
    public R ufindAllormodil(@RequestBody(required = false) uVO sVO){

        PageHelper.startPage(sVO.getPage(),sVO.getLimit());

        List<U> slist = studenService.finduAll(sVO);
        //创建分页对象
        PageInfo<U> pageInfo = new PageInfo<U>(slist);

        log.info("List<studentInformation>:::"+pageInfo);

        return  R.ok().data("total",pageInfo.getTotal()).data("rows",pageInfo.getList());
    }




    //下载
    @GetMapping("/read")
    public R read(){
        List<stuInfoData> alist = studenService.select();//全查
//        log.info("aaaaaaaaa"+alist);
        String fileName = "D:\\excel\\stu.xlsx";
        EasyExcel.write(fileName,stuInfoData.class).sheet("学生列表").doWrite(alist);
        return R.ok();
    }


//    添加课程分类
//    获取上传过来的文件，把文件内容读取出来
    @PostMapping("/addSubject")
    public R addSubject(MultipartFile file){
        //上传文件过来的Excel文件 上传固定的地址
//        eduSubjectService.addSubject(file,eduSubjectService);
//        String fileName = "D:\\excel\\stu.xlsx";
//        studenService.addStuExel(fileName,studenService);
        //动态地址
        studenService.addStuExel(file,studenService);
        return R.ok();
    }



//    @GetMapping("/readuu")
//    public R readuu(){
//        List<Userte> alist = studenService.selectuu();
//        log.info("aaaaaaaaa"+alist);
//        String fileName = "D:\\excel\\user.xlsx";
//        EasyExcel.write(fileName,Userte.class).sheet("学生列表").doWrite(alist);
//        return R.ok();
//    }
//    //    添加课程分类
////    获取上传过来的文件，把文件内容读取出来
//    @GetMapping("/addUser")
//    public R addUser(){
//        //上传文件过来的Excel文件
////        eduSubjectService.addSubject(file,eduSubjectService);
//        String fileName = "D:\\excel\\user.xlsx";
//
//        studenService.addUser(fileName,studenService);
//        return R.ok();
//    }



}
